package com.bbdjt.weixin.web.action.bespoke;


import org.apache.log4j.Logger;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.bbdjt.weixin.base.action.BaseAction;
import com.bbdjt.weixin.model.Bespoke;
import com.bbdjt.weixin.model.BespokeQueryModel;
import com.bbdjt.weixin.service.BespokeService;
import com.bbdjt.weixin.web.action.patient.PatientAction;

/**
 * @项目名称: bbdjt
 * @类名称: BespokeAction
 * @类描述: Bespoke表相关操作
 * @创建人: guoLiang
 * @创建时间: 2017-01-18 04:37:41
 * @修改人: 
 * @修改时间: 
 * @修改备注:
 * @version
 */
@Controller
public class BespokeAction extends BaseAction {
	
	@Autowired
	private BespokeService bespokeService;

	private String page_list = "jsp/bespoke/bespokeindex";

	private String redirect_list = "redirect:r_bespoke";
	
	private static Logger logger = Logger.getLogger(BespokeAction.class);
	
	
	// 分页查询
	@RequestMapping(value = "r_bespoke")
	public ModelAndView getListPage(BespokeQueryModel bespoke) {
		ModelAndView result = new ModelAndView(page_list);
		bespoke.setOrderBy("id asc");
		List<Bespoke> bespokeList = bespokeService.selectByBespoke(bespoke);
		result.addObject("pageInfo", new PageInfo<Bespoke>(bespokeList));
		result.addObject("bespoke", bespoke);
		return result;
	}

	// 自定义分页查询 根据xml 分页查询
	@RequestMapping(value = "r_bespokeXml")
	public ModelAndView getListPageXml(BespokeQueryModel bespoke) {
		ModelAndView result = new ModelAndView(page_list);
		bespoke.setOrderBy("id asc");
		PageInfo<BespokeQueryModel> pageInfo = bespokeService.selectByBespokeQueryModel(bespoke);
		List<BespokeQueryModel> bespokeList = pageInfo.getList();
		result.addObject("pageInfo", new PageInfo<BespokeQueryModel>(bespokeList));
		result.addObject("bespoke", bespoke);
		return result;
	}

	// 返回整个页面的 json 包括分页 也签 内容 标示 和 正文 内容 同上方法 只不过是返回json
	@RequestMapping(value = "r_bespokeJson", produces = "application/json")
	public ModelAndView pageInfo(BespokeQueryModel bespoke) {
		ModelAndView result = new ModelAndView();
		PageInfo<BespokeQueryModel> pageInfo = bespokeService.selectByBespokeQueryModel(bespoke);
		result.addObject("pageInfo", pageInfo);
		return result;
	}
	
	//ligerui table
	@ResponseBody
	@RequestMapping(value = "r_bespokeLigerUi", produces="application/json;charset=UTF-8") 
	public String pageInfoLigerUi(BespokeQueryModel bespoke) {
		bespoke.setOrderBy(bespoke.getSortname(),bespoke.getSortorder());
		PageInfo<BespokeQueryModel> pageInfo = bespokeService.selectByBespokeQueryModel(bespoke);
		return super.toLigerUiSelectJson(pageInfo);
	}

	// 添加数据 添加或者修改数据页面 主键为id
	// 如果没有id 不查 如果有查询补上信息
	@RequestMapping(value = "v_bespoke")
	public ModelAndView add(Bespoke bespoke) {
		ModelAndView result = new ModelAndView();
		if (bespoke.getId() != null) {
			bespoke = bespokeService.selectByKey(bespoke.getId());
		}
		result.addObject("bespoke", bespoke);
		return result;
	}

	// 如果有id就更新 没有 就插入
	@RequestMapping(value = "c_bespoke")
	public ModelAndView save(Bespoke bespoke) {
		ModelAndView result = new ModelAndView(redirect_list);
		if (bespoke.getId() != null) {
			bespokeService.updateNotNull(bespoke);
		} else {
			// 会保存null 值
			bespokeService.insertSelective(bespoke);
		}
		return result;
	}

	// 删除数据
	@RequestMapping("d_bespoke")
	public String delete(Integer id) {
		bespokeService.delete(id);
		return redirect_list;
	}
	
	//患者预约课程
	@ResponseBody
	@RequestMapping(value = "addBespoke")
	public String addBespoke(Bespoke bespoke){
		if(bespoke.getPatientId() != null && bespoke.getSyllabusId() != null){
			bespoke.setCreateDate(new Date());
			bespokeService.insertSelective(bespoke);
			return super.writeOK(bespoke);
		}else{
			logger.info("患者ID或课程ID为空:" + bespoke.getPatientId() + "," + bespoke.getSyllabusId());
			return super.lackError();
		}
	}
	
}